# -*- coding: utf-8 -*-

'''
This file is a textobot Simple GUI plugin.
'''

import os
import json
import time

# it's only for VSCode Textobot-Run command
# turn to 0 before you pack this plugin
# turn to 1 if you want to run directly from VSCode
dev_mode = 0

# turn to 1 when you finished the plugin development
# and run at least one time to flush remote server side global code 
# otherwise will conflict with the release TB plugin
dev_clear_hotcode = 1

flag_tbsubmit = 'Textobot Submitting'
flag_sep = '---'
flag_tbgui = 'TBGUI'

if not dev_mode:
    # this plugin package's root directory
    pkg_root = os.path.dirname(__file__)

    # pickup this plugin's version dynamically
    with open(pkg_root + '/textobot.json', 'r') as fp:
        pkginfo = json.load(fp)
        pkg_version = pkginfo['version']

def contain_text(points, texts, t):
    for i in range(0, len(texts)):
        if texts[i].find(t) >= 0:
            return points[i]
    return None

# handler for text views
def proc_event_views(pts, txts):
    pass

# handler for dialog
def proc_event_dialog(pts, txts):
    # process submitting dialog
    if contain_text(pts, txts, flag_tbsubmit):
        # click off submitting dialog
        TB_touch_pair(contain_text(pts, txts, '.OK'))
        time.sleep(1)
        for i in range(0, len(txts)):
            # find our input flag
            if txts[i].find(flag_tbgui) > 0:
                lines = txts[i].split('\n')
                result = ''
                for l in lines:
                    items = l.split(flag_sep)
                    if len(items) != 3:
                        continue
                    print(items)
                    key = items[1]
                    value = items[2]
                    # parse result
                    if key.find('program') > 0:
                        result += 'The selected program language is { ' + value + ' }.\n\n'
                    elif key.find('checkbox') > 0:
                        result += 'The checkbox state is { ' + value + ' }.\n\n'
                    elif key.find('edit input') > 0:
                        result += 'The input string is { ' + value + ' }.\n\n'
                    elif key.find('number') > 0:
                        result += 'The number value is { ' + value + ' }.\n\n'
                TB_dialog(flag_tbgui + ' Result', result)
                break

# handler for event
def TB_on_event(dictarg):
    event = dictarg['event']
    if event == 'texts':
        proc_event_views(dictarg.keys(), dictarg.values())
    elif event == 'dlgtexts':
        proc_event_dialog(dictarg.keys(), dictarg.values())
    return {}

def show_gui():
    TB_gui(
'''
label---Simple %s---center
padding
edit---%s edit input---Textobot
check---%s checkbox---1
combo---%s program---C++,ObjC,Python,JavaScript
spin---%s number---0,1,10
''' % (flag_tbgui, flag_tbgui, flag_tbgui, flag_tbgui, flag_tbgui))

# handler for main
def TB_main(dictarg):
    event = dictarg['event']
    if event == 'start':
        show_gui()
    return {}

# it's only for VSCode Textobot-Run command, can only recv TB_on_event
if dev_mode:
    TB_clear_log()
    # only available in dev mode
    if dev_clear_hotcode:
        TB_event_callback = None
        print('Cleared dev hot code...')
    else:
        TB_event_callback = TB_on_event
        show_gui()
else:
    print('The SimpleGUI plugin(%s) is waiting for event ...' % (pkg_version))
